using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Globalization;

namespace Umdf.SideShow.Scf.ObjectModel
{
    public class ScfText : ScfObject
    {
        private ScfAlignment _alignment;
        private bool _wrap;
        private string _color = "000000";
        private List<ScfTextData> _text;

        public ScfAlignment Aligment
        {
            get { return _alignment; }
            set { _alignment = value; }
        }

        public bool Wrap
        {
            get { return _wrap; }
            set { _wrap = value; }
        }

        public string Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public List<ScfTextData> Text
        {
            get
            {
                if (_text == null)
                {
                    _text = new List<ScfTextData>();
                }
                return _text;
            }
        }

        protected internal override void DeserializeFromXml(XmlNode node)
        {
            base.DeserializeFromXml(node);
            _alignment = GetAttribute<ScfAlignment>(node, "align");
            _wrap = GetAttribute<bool>(node, "wrap");
            _color = GetAttribute<string>(node, "clr", "000000");
            _text = new List<ScfTextData>();

            XmlNode n = node.FirstChild;
            ScfTextData data = new ScfTextData();
            data.Color = System.Drawing.Color.FromArgb(int.Parse(this.Color, NumberStyles.HexNumber));
            Color previousClr = data.Color;
            bool end = false;
            while (!end)
            {
                if (n.NodeType == XmlNodeType.Text)
                {
                    data.Text += n.Value;
                }
                else if (n.LocalName == "em")
                {
                    if (!data.Emphasized)
                    {
                        if (!string.IsNullOrEmpty(data.Text))
                        {
                            _text.Add(data);
                            data = new ScfTextData("", data.Color, true);
                        }
                        data.Emphasized = true;

                    }
                }
                else if (n.LocalName == "clr")
                {
                    Color clr = System.Drawing.Color.FromArgb(int.Parse(n.Attributes["rgb"].Value, NumberStyles.HexNumber));
                    if (data.Color != clr)
                    {
                        previousClr = data.Color;
                        if (!string.IsNullOrEmpty(data.Text))
                        {
                            _text.Add(data);
                            data = new ScfTextData("", clr, data.Emphasized);
                        }
                        data.Color = clr;
                    }
                }
                else if (n.LocalName == "br")
                {
                    _text.Add(data);
                    _text.Add(new ScfTextData(true));
                    data = new ScfTextData("", data.Color, data.Emphasized);
                }
                if (n.HasChildNodes)
                {
                    n = n.FirstChild;
                }
                else if (n.NextSibling != null)
                {
                    n = n.NextSibling;
                }
                else if (n.ParentNode != node)
                {
                    n = n.ParentNode;
                    if (n.NextSibling != null)
                    {
                        _text.Add(data);
                        if (n.LocalName == "em")
                        {
                            data = new ScfTextData("", data.Color, false);
                        }
                        else if (n.LocalName == "clr")
                        {
                            data = new ScfTextData("", previousClr, data.Emphasized);
                        }
                        n = n.NextSibling;
                    }
                    else
                    {
                        end = true;
                    }
                }
                else
                {
                    end = true;
                }
            }
            _text.Add(data);

        }

        protected internal override void SerializeObject(System.IO.BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write((int)_alignment);
            writer.Write(_wrap);
            writer.Write(_color);
            writer.Write(Text.Count);
            foreach (ScfTextData td in Text)
            {
                writer.Write(td.GetType().FullName);
                td.SerializeObject(writer);
            }
        }

        protected internal override void DeserializeObject(System.IO.BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _alignment = (ScfAlignment)reader.ReadInt32();
            _wrap = reader.ReadBoolean();
            _color = reader.ReadString();
            int count = reader.ReadInt32();
            _text = new List<ScfTextData>();
            for (int i = 0; i < count; i++)
            {
                string typeName = reader.ReadString();
                Type t = Type.GetType(typeName);
                ScfTextData td = (ScfTextData)Activator.CreateInstance(t);
                td.DeserializeObject(reader);
                _text.Add(td);
            }
        }
    }
}
